package com.javachmapi.springbootpoi.controller;

import com.javachmapi.springbootpoi.service.ExcelService;
import com.javachmapi.springbootpoi.vo.ResponseData;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * (User)表控制层
 * @author 宋利军
 */
@RestController
@RequestMapping("/user")
@Api(value = "UserController", tags = "用户信息导入导出", description = "")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private ExcelService excelService;

    /**
     * 功能：导出
     * 描述：文件格式为xls或xlsx
     * @param response
     * @param fileName 导出文件名称
     * @param pageSize 查询起始位置
     * @param pageNum 查询条数
     * @return
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出Execl", notes = "导出Execl")
    public ResponseData exportExcel(HttpServletResponse response,
                                    @RequestParam(name = "fileName") @ApiParam(value = "导出文件名称", defaultValue = "export.xls", required = true) String fileName,
                                    @RequestParam(name = "pageSize") @ApiParam(value = "查询起始位置，默认从0开始", defaultValue = "0", required = true) Integer pageSize,
                                    @RequestParam(name = "pageNum") @ApiParam(value = "查询条数，默认查询5条", defaultValue = "5", required = true) Integer pageNum) {
        if (fileName == null || "".equals(fileName)) {
            return ResponseData.failure("文件名不能为空！");
        } else {
            if (fileName.endsWith("xls") || fileName.endsWith("xlsx")) {
                Boolean isOk = excelService.exportExcel(response, fileName, pageSize, pageNum);
                if (isOk) {
                    return ResponseData.success("导出成功！");
                } else {
                    return ResponseData.failure("导出失败！");
                }
            }
            return ResponseData.failure("文件格式有误！");
        }
    }

    /**
     * 功能：导入
     * 描述：文件格式为xls或xlsx
     * @param file
     * @throws IOException
     */
    @PostMapping("/import")
    @ApiOperation(value = "导入Execl", notes = "导入Execl")
    public ResponseData importExcel(MultipartFile file) throws IOException {
        //获取文件的后缀名
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //拼接文件名称，32位随机uuid+后缀名
        String fileName = UUID.randomUUID().toString().replace("-", "").toLowerCase() + suffix;
        if (fileName == null && "".equals(fileName)) {
            return ResponseData.failure("文件名不能为空！");
        } else {
            if (fileName.endsWith("xls") || fileName.endsWith("xlsx")) {
                InputStream inputStream = file.getInputStream();
                Boolean isOk = excelService.importExcel(inputStream,fileName);
                if (isOk) {
                    return ResponseData.success("导入成功！");
                } else {
                    return ResponseData.failure("导入失败！");
                }
            }
            return ResponseData.failure("文件格式错误！");
        }
    }
}